package net.topicopedia.fixtures;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Text;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

public class Fixtures {
	
	protected Boolean testMode = true;
	
	protected Boolean addTagToCategory ( List<String> ctags, Entity category, DatastoreService datastore ) throws JSONException
	{
		Text tagsText = (Text) category.getProperty("tags");
		String tagsString = tagsText.getValue();
		JSONObject tagsJSON = new JSONObject();
		JSONArray tags;
		if ( tagsString == "" )
		{
			tagsJSON = new JSONObject();
			tags = new JSONArray();
		} else {
			tagsJSON = new JSONObject(tagsString);
			tags = tagsJSON.getJSONArray("tags");
		}
		
		for ( String string : ctags )
		{
			Entity tag = new Entity("Tag");
			tag.setProperty("name", string);
			tag.setProperty("category", category.getKey().getId()+"" );
			tag.setProperty("topics", new Text("") );
			tag.setProperty("count", 0 );
			Key tagKey = datastore.put( tag );
			
			JSONObject tagJSON = new JSONObject();
			tagJSON.put("uuid", tagKey.getId()+"");
			tagJSON.put("name", string);
			tagJSON.put("count", 0);
			tags.put(tagJSON);
			
			tagsJSON.put ( "tags", tags );
		}
		
		category.setProperty("tags", new Text ( tagsJSON.toString() ) );
		
		datastore.put(category);
		
		return true;
	}
	
	public Boolean createTags () throws JSONException
	{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

		List< List<String> > ctags = new ArrayList< List<String> >();
		
		List<String> ctags1 = new ArrayList<String>();
		ctags1.add("Painter");
		ctags1.add("Writer");
		ctags1.add("Poet");
		ctags1.add("Sculptor");
		ctags1.add("Chinese");
		ctags1.add("Canadian");
		ctags1.add("Indian");
		ctags1.add("American");
		ctags1.add("European");
		ctags1.add("Latin Amwrican");
		ctags.add(ctags1);
		List<String> ctags2 = new ArrayList<String>();
		ctags2.add("Politics");
		ctags2.add("History");
		ctags2.add("Canada");
		ctags2.add("USA");
		ctags2.add("Europe");
		ctags2.add("Culture");
		ctags2.add("Festival");
		ctags2.add("War");
		ctags.add(ctags2);
		List<String> ctags3 = new ArrayList<String>();
		ctags3.add("Asia");
		ctags3.add("Australia");
		ctags3.add("Europe");
		ctags3.add("Africa");
		ctags3.add("North America");
		ctags3.add("South America");
		ctags3.add("Volcano");
		ctags3.add("River");
		ctags3.add("Mountain");
		ctags3.add("Ocean");
		ctags3.add("Pacific");
		ctags3.add("Atlantic");
		ctags3.add("Indian");
		ctags3.add("Sea");
		ctags3.add("Universe");
		ctags3.add("Planet");
		ctags3.add("Star");
		ctags3.add("Moon");
		ctags.add(ctags3);
		List<String> ctags4 = new ArrayList<String>();
		ctags4.add("Organ");
		ctags4.add("Muscle");
		ctags4.add("Bone");
		ctags4.add("System");
		ctags4.add("Respiratory");
		ctags4.add("Reproduction");
		ctags4.add("Digestion");
		ctags4.add("Musculoskeletal");
		ctags4.add("Musculoskeletal");
		ctags.add(ctags4);
		List<String> ctags5 = new ArrayList<String>();
		ctags5.add("Bird");
		ctags5.add("Dinosaur");
		ctags5.add("Insect");
		ctags5.add("Mammal");
		ctags5.add("Reptile");
		ctags5.add("Extinct");
		ctags5.add("Endangered");
		ctags5.add("Species");
		ctags5.add("Tree");
		ctags5.add("Fungus");
		ctags5.add("Fruit");
		ctags5.add("Flower");
		ctags5.add("Vegetable");
		ctags5.add("Nut");
		ctags.add(ctags5);
		List<String> ctags6 = new ArrayList<String>();
		ctags6.add("Cytology");
		ctags6.add("Botany");
		ctags6.add("Botany");
		ctags6.add("Zoology");
		ctags6.add("Chemistry");
		ctags6.add("Element");
		ctags6.add("Compound");
		ctags6.add("Chemical Processes");
		ctags6.add("Motion");
		ctags6.add("Physical Structure");
		ctags6.add("Mechanics");
		ctags6.add("Numbers");
		ctags.add(ctags6);
		List<String> ctags7 = new ArrayList<String>();
		ctags7.add("Olympics");
		ctags7.add("Athletics");
		ctags7.add("Chess");
		ctags7.add("Music");
		ctags7.add("Film");
		ctags.add(ctags7);
		List<String> ctags8 = new ArrayList<String>();
		ctags8.add("Engineering");
		ctags8.add("Tunnels");
		ctags8.add("Bridges");
		ctags8.add("Trains");
		ctags8.add("Car");
		ctags8.add("Rocket");
		ctags8.add("Ship");
		ctags.add(ctags8);
		
		int len = 8;
		
		if ( testMode )
		{
			len = 1;
		}
		
		for ( int i=0; i< len; i++ )
		{

			Key categoryKey = KeyFactory.createKey("Category", 1001 + i );
			Entity category = null;
			try {
				category = datastore.get(categoryKey);
			} catch (EntityNotFoundException e) {
				e.printStackTrace();
			}
			if ( category != null )
			{
				addTagToCategory ( ctags.get(i), category,  datastore );
			}
		}
		return true;
	}

	public List<String> getCategoryNames ()
	{
		List<String> list = new ArrayList<String>();
		list.add("Arts and Literature");
		
		if ( testMode )
		{
			return list;
		}
		list.add("Social Science");
		list.add("Geography");
		list.add("Human Body and Health");
		list.add("Biosphere");
		list.add("Sciences and Mathematics");
		list.add("Sports");
		list.add("Technology");
		
		return list;
	}
	
	
	public Boolean createCategories ()
	{
		List<String> list = getCategoryNames ();
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		
		int keyId = 1000;
		
		for ( String string : list )
		{
			keyId++;
			Entity category = new Entity("Category", keyId);
			category.setProperty("name", string);
			category.setProperty("count", 0);
			category.setProperty("tags", new Text("") );
			datastore.put( category );
		}
		
		
		Query query = new Query("Category");
		
		PreparedQuery pq = datastore.prepare(query);
		
		int count = 0;
		
		for (Entity result : pq.asIterable() ) {
			
			Boolean exists = false;
			String storedName = (String) result.getProperty("name");
			for ( String name : list )
			{
				if ( name.matches( storedName ) )
				{
					exists = true;
				}
			}
			
			if ( exists )
			{
				count++;
			}
		}
		
		if ( count == list.size() ){
			return true;
		}
		
		return false;
	}
	
}
